package com.haofengsoft.scce.controller.consultation;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.haofengsoft.scce.adapter.information.NanJingModel;
import com.haofengsoft.scce.db.biz.consultation.FocusNewBiz;
import com.haofengsoft.scce.db.biz.consultation.NanJingBiz;
import com.haofengsoft.scce.db.entity.consultation.NanJing;

/**
 * Created by admin on 2015/7/14.
 */
public class NanJingController {
    private static NanJingController instance = null;
    private Context appContext;

    public static synchronized NanJingController getInstance(Context context) {
        if (instance == null) {
            instance = new NanJingController(context);
        }
        return instance;
    }

    public NanJingController(Context appContext) {
        this.appContext = appContext;
    }


    public List<NanJingModel> gainNanJingModels() {
        List<NanJing> NanJingList = NanJingBiz.getInstance(appContext).gainNanjingList();
        List<NanJingModel> nanjingModelList = new ArrayList<NanJingModel>();
        for (NanJing nanJing : NanJingList) {
            NanJingModel model = new NanJingModel();
            model.setId(nanJing.getId());
            model.setTime(nanJing.getStrCreateDate());
            model.setDetail(nanJing.getSummary());
            model.setTitle(nanJing.getTitle());
            nanjingModelList.add(model);
        }
        return nanjingModelList;
    }

    // 处理要闻
    public List<NanJingModel> handleNanJing(String json) {
        try {
            List<NanJingModel> modelList = new ArrayList<NanJingModel>();
            List<NanJing> nanJingList = new ArrayList<NanJing>();
            JSONArray newsModelArray = JSON.parseObject(json).getJSONArray("NewList");
            for (int i = 0; i < newsModelArray.size(); i++) {
                JSONObject newsModelObj = newsModelArray.getJSONObject(i);
                NanJing nanjingModel = new NanJing();
                nanjingModel.setTitle(newsModelObj.getString("Title"));
                nanjingModel.setSummary(newsModelObj.getString("Summary"));
                nanjingModel.setStrCreateDate(newsModelObj.getString("StrCreateDate"));
                nanjingModel.setId(newsModelObj.getString("ID"));
                nanJingList.add(nanjingModel);
            }
            //首次加载，清楚掉原来的数据
            if (nanJingList.size() > 1) {
                NanJingBiz.getInstance(appContext).deleteAll();
                for (NanJing nanJing : nanJingList) {
                    NanJingModel model = new NanJingModel();
                    model.setId(nanJing.getId());
                    model.setTime(nanJing.getStrCreateDate());
                    model.setDetail(nanJing.getSummary());
                    model.setTitle(nanJing.getTitle());
                    modelList.add(model);
                }
                //添加数据库，第一次，和下拉的
                NanJingBiz.getInstance(appContext).insertNanjingList(nanJingList);
            } else {
                modelList = null;
            }
            return modelList;
        } catch (Exception e) {
            return null;
        }
    }
}
